<!--
 * @Author: your name
 * @Date: 2020-12-09 14:41:52
 * @LastEditTime: 2020-12-21 16:07:10
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \sende_nuxt\pages\personalCenter\accountDashboard\accountInformation\index.vue
-->
<template>
  <div class="accountInformation ptb30 plr20 h100 spacing relative">
    <el-form :hide-required-asterisk="true" :model="ruleForm" label-position="top" :rules="rules" ref="ruleForm" label-width="100px">
      <div class="information-ruleForm">
        <div class="accountInformation_left s48">
          <h3 class="accountInformation_title size16 mb10">{{$t('ACCOUNT INFORMATION')}}</h3>
            <el-form-item class="mb10" label="First Name" prop="firstname">
              <el-input v-model="ruleForm.firstname"></el-input>
            </el-form-item>
            <el-form-item class="mb10" label="Last Name" prop="lastname">
              <el-input v-model="ruleForm.lastname"></el-input>
            </el-form-item>
            <el-form-item class="mb0">
              <el-checkbox label="Change Email" v-model="changeEmail" @change="handleCheckEmail"></el-checkbox>
            </el-form-item>
            <el-form-item class="mb0">
              <el-checkbox label="Change Password" v-model="changePass" @change="handleCheckPass"></el-checkbox>
            </el-form-item>
        </div>
        <div class="accountInformation_right s48">
          <h3 class="accountInformation_title size16 mb10" v-if="changeEmail && changePass">{{$t('CHANGE EMAIL AND PASSWORD')}}</h3>
          <h3 class="accountInformation_title size16 mb10" v-if="changeEmail && !changePass">{{$t('CHANGE EMAIL')}}</h3>
          <h3 class="accountInformation_title size16 mb10" v-if="changePass && !changeEmail">{{$t('CHANGE PASSWORD')}}</h3>
            <el-form-item class="mb10" label="Email" prop="email" v-if="changeEmail">
              <el-input v-model="ruleForm.email"></el-input>
            </el-form-item>
            <el-form-item class="mb10" label="Current Password" prop="curPass" v-if="changeEmail || changePass">
              <el-input v-model="ruleForm.curPass" type="password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item class="mb10" label="New Password" prop="newPass" v-if="changePass">
              <el-input v-model="ruleForm.newPass" type="password" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item class="mb10" label="Confirm New Password" prop="checkPass" v-if="changePass">
              <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off"></el-input>
            </el-form-item>
        </div>
      </div>
      <p class="size12 mtb10">{{$t('If you created this account using Amazon Pay, you might not know your site password. Request a password to change your account password.')}}</p>
      <el-form-item>
        <el-button class="ptb0 plr10" @click="submitForm('ruleForm')" type="info" plain>{{$t('SAVE')}}</el-button>
      </el-form-item>
    </el-form>
    <back class="mb30" />
  </div>
</template>

<script>
import Back from '~/components/personalCenter/Back'
export default {
  data() {
      var validateNewPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('This is a required field.'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validateCheckPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('Please enter the password again'));
        } else if (value !== this.ruleForm.newPass) {
          callback(new Error('The two passwords are inconsistent!'));
        } else {
          callback();
        }
      };
    return {
      changeEmail: '',
      changePass: '',
      ruleForm: {
        firstname: '',
        lastname: '',
        email: '',
        curPass: '',
        newPass: '',
        checkPass: ''
      },
      rules: {
        firstname: [
          { required: true, message: 'This is a required field.', trigger: 'blur' },
        ],
        lastname: [
          { required: true, message: 'This is a required field.', trigger: 'blur' },
        ],
        curPass: [
          { required: true, message: 'This is a required field.', trigger: 'blur' },  
        ],
        newPass: [
          { required: true, validator: validateNewPass, trigger: 'blur' },  
        ],
        checkPass: [
          { required: true, validator: validateCheckPass, trigger: 'blur' },  
        ],
        email: [
          { required: true, message: 'please enter email address', trigger: 'blur' },
          { type: 'email', message: 'Please enter the correct email address', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  components: {
    Back
  },
  methods: {
    handleCheckEmail(value) {
      console.log('value :>> ', value);
      console.log('this.changeEmail :>> ', this.changeEmail);
    },
    handleCheckPass(value) {
      console.log('value :>> ', value);
      console.log('changePass :>> ', this.changePass);
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!');
           this.$refs[formName].resetFields();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }
}
</script>

<style lang="scss" scoped>
@import "~/assets/scss/index.scss";
.accountInformation { border: 1px solid $ddd;}
.information-ruleForm {
  @include flex();
  justify-content: space-between;
  
}
</style>
